package cn.paul.wechatshop.controller;

import cn.paul.wechatshop.common.Const;
import cn.paul.wechatshop.common.ServerResponse;
import cn.paul.wechatshop.dto.OrderDTO;
import cn.paul.wechatshop.exception.WechatshopException;
import cn.paul.wechatshop.form.OrderForm;
import cn.paul.wechatshop.service.IBuyerService;
import cn.paul.wechatshop.service.IOrderMasterService;
import cn.paul.wechatshop.service.impl.BuyerServiceImpl;
import com.google.common.collect.Maps;
import com.mysql.fabric.Server;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author 徐凌峰
 * @date 2017/12/12 0012
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private IOrderMasterService iOrderMasterService;
    @Autowired
    private IBuyerService iBuyerService;


    /**
     * 创建订单
     *
     * @param orderForm
     * @param result
     * @return
     */
    @PostMapping("create")
    public ServerResponse<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult result) {
        if (result.hasErrors()) {
            log.error("[创建订单]参数不正确,orderForm={}", orderForm);
            throw new WechatshopException(Const.ResultEnum.PARAM_ERROR.getCode(), result.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = new OrderDTO();
        Map<String, String> map = Maps.newHashMap();
        map.put("orderId", iOrderMasterService.create(orderForm));
        return ServerResponse.createBySuccess("成功", map);
    }

    /**
     * 订单列表
     *
     * @param openid
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list")
    public ServerResponse list(@RequestParam("openid") String openid,
                               @RequestParam(defaultValue = "0", value = "page") Integer page,
                               @RequestParam(defaultValue = "10", value = "size") Integer size) {
        if (StringUtils.isBlank(openid)) {
            log.error("[订单列表]参数不正确,openid={}", openid);
            throw new WechatshopException(Const.ResultEnum.PARAM_ERROR);
        }
        return iOrderMasterService.findList(openid, new PageRequest(page, size));
    }

    /**
     * 订单详情
     *
     * @param openid
     * @param orderId
     * @return
     */
    @GetMapping("detail")
    public ServerResponse<OrderDTO> detail(String orderId, String openid) {
        return ServerResponse.createBySuccess(iBuyerService.find(openid,orderId));
    }

    /**
     * 取消订单
     *
     * @param openid
     * @param orderId
     * @return
     */
    @PostMapping("cancel")
    public ServerResponse cancel(String orderId, String openid) {
        return iBuyerService.cancel(orderId, openid);
    }

}
